﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject;
using System.Reflection;
using BlogPlat.Model.Abstract;
using BlogPlat.Model.Repositories;
using System.Configuration;
using Ninject.Web.Mvc;
using Ninject.Modules;
using BlogPlat.Model;

namespace BlogPlat
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : NinjectHttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            
            routes.MapRoute(
                "PostTagged",
                "post/tagged/{friendlyUrl}",
                new { controller = "Post", action = "Tagged" },
                new string[] { "BlogPlat.Controllers" }
            );

            routes.MapRoute(
                "PostYearMonthDay",
                "post/{year}/{month}/{day}",
                new { controller = "Post", action = "Index" },
                new { year = @"\d{4}" },
                new string[] { "BlogPlat.Controllers" }

            );
                       
            routes.MapRoute(
                "PostYearMonth",
                "post/{year}/{month}",
                new { controller = "Post", action = "Index" },
                new { year = @"\d{4}" },
                new string[] { "BlogPlat.Controllers" }
            );

            routes.MapRoute(
                "PostYear",
                "post/{year}",
                new { controller = "Post", action = "Index" },
                new { year = @"\d{4}" },
                new string[] { "BlogPlat.Controllers" }
            );

            routes.MapRoute(
                "PostFriendlyUrl",
                "post/{friendlyUrl}",
                new { controller = "Post", action = "FriendlyUrl" },
                new string[] { "BlogPlat.Controllers" }
            );

            routes.MapRoute("Category", "{categorylist}",
                new { controller = "Category", action = "Index" },
                new { categorylist = @"[0-9a-zA-Z]*" },
                new string[] { "BlogPlat.Controllers" }
                );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Post", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new string[] { "BlogPlat.Controllers" }
            );
        }

        protected override void OnApplicationStarted() 	
        {
            base.OnApplicationStarted();                        
            
            RegisterAllControllersIn(Assembly.Load("BlogPlat"), c => GetControllerName(c), c => string.Equals(c.Namespace, "BlogPlat.Controllers"));
            RegisterAllControllersIn(Assembly.Load("BlogPlat"), c => GetControllerName(c), c => string.Equals(c.Namespace, "BlogPlat.Areas.Admin.Controllers"));

            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);
        }

        protected override IKernel CreateKernel()
        {
            return new StandardKernel(new BlogPlatNinjectModule());
        }
    }

    public class BlogPlatNinjectModule : NinjectModule
    {
        public override void Load()
        {
            Bind<IPostRepository>()
                .To<PostRepository>()
                .WhenInjectedInto(typeof(BlogPlat.Controllers.PostController))
                .WithConstructorArgument("connString",
                ConfigurationManager.ConnectionStrings["BlogPlatConnString"].ConnectionString
                );

            Bind<IPostRepository>()
                .To<PostRepository>()
                .WhenInjectedInto(typeof(BlogPlat.Areas.Admin.Controllers.PostController))
                .WithConstructorArgument("connString",
                ConfigurationManager.ConnectionStrings["BlogPlatConnString"].ConnectionString
                );

            Bind<IRepository<Category>>()
                .To<CategoryRepository>()
                .WhenInjectedInto(typeof(BlogPlat.Areas.Admin.Controllers.CategoryController))
                .WithConstructorArgument("connString",
                ConfigurationManager.ConnectionStrings["BlogPlatConnString"].ConnectionString
                );

            Bind<IRepository<Tag>>()
                .To<TagRepository>()
                .WhenInjectedInto(typeof(BlogPlat.Areas.Admin.Controllers.TagController))
                .WithConstructorArgument("connString",
                ConfigurationManager.ConnectionStrings["BlogPlatConnString"].ConnectionString
                );
        }
    }
}